home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / hpgl2ps.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1989-08-08  |  1KB  |  58 lines

  1. /*    getopt.c    */
  2. #include <stdio.h>
  3. #define ERR(s, c)    if(opterr){fputs(argv[0],stderr);fputs(s,stderr);fputc(c,stderr);fputc('\n',stderr);}
  4.  
  5. extern int strcmp();
  6. extern char *strchr();
  7.  
  8. int    opterr = 1;
  9. int    optind = 1;
  10. int    optopt;
  11. char    *optarg;
  12.  
  13. int
  14. getopt(argc, argv, opts)
  15. int    argc;
  16. char    **argv, *opts;
  17. {
  18.     static int sp = 1;
  19.     register int c;
  20.     register char *cp;
  21.  
  22.     if(sp == 1)
  23.         if(optind >= argc ||
  24.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  25.             return(EOF);
  26.         else if(strcmp(argv[optind], "--") == NULL) {
  27.             optind++;
  28.             return(EOF);
  29.         }
  30.     optopt = c = argv[optind][sp];
  31.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  32.         ERR(": illegal option -- ", c);
  33.         if(argv[optind][++sp] == '\0') {
  34.             optind++;
  35.             sp = 1;
  36.         }
  37.         return('?');
  38.     }
  39.     if(*++cp == ':') {
  40.         if(argv[optind][sp+1] != '\0')
  41.             optarg = &argv[optind++][sp+1];
  42.         else if(++optind >= argc) {
  43.             ERR(": option requires an argument -- ", c);
  44.             sp = 1;
  45.             return('?');
  46.         } else
  47.             optarg = argv[optind++];
  48.         sp = 1;
  49.     } else {
  50.         if(argv[optind][++sp] == '\0') {
  51.             sp = 1;
  52.             optind++;
  53.         }
  54.         optarg = NULL;
  55.     }
  56.     return(c);
  57. }
  58.